package com.example.multithread.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by wangyg6 on 2020/8/11 11:21.
 * 线程池工厂有问题
 */
@Service
public class TestService {
	private static final Logger log = LoggerFactory.getLogger(TestService.class);
	private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 10, 60, TimeUnit.SECONDS,
			new ArrayBlockingQueue<Runnable>(100), /*new ThreadFactory() {
		AtomicInteger integer = new AtomicInteger(1);

		@Override
		public Thread newThread(Runnable r) {
			Thread t = new Thread(new ThreadGroup("TestGroup"), r, "test" + integer.getAndIncrement(), 0);
			if (t.isDaemon())
				t.setDaemon(false);
			if (t.getPriority() != Thread.NORM_PRIORITY)
				t.setPriority(Thread.NORM_PRIORITY);
			return t;
		}
	}, */(r, executor) -> {
		if (!executor.isShutdown()) {
			r.run();
		}
	});

	static {
		if (threadPool != null) {
			threadPool.prestartAllCoreThreads();
			log.debug("线程池已启动");
		}
	}

	public ThreadPoolExecutor getThreadPool() {
		return threadPool;
	}
}
